/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.vdab.web;

import be.vdab.entities.Filiaal;
import be.vdab.services.FiliaalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author lok1105
 */
@Controller
@RequestMapping("/filialen")
class FiliaalController {
    
    private final FiliaalService filiaalService;
     
    @Autowired
    public FiliaalController (FiliaalService filiaalService) {
        this.filiaalService = filiaalService;
    }
    
    /*@RequestMapping(method = RequestMethod.GET)
    public String findAll(Model model) {
        model.addAttribute("filialen", filiaalService.findAll());
        return "filialen/filialen";
    }*/
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView findAll() {
        /*ModelAndView modelAndView = new ModelAndView("filialen/filialen", "filialen", 
             filiaalService.findAll());
        modelAndView.addObject("werknemers", werknemerService.findAll());
        return modelAndView;*/
        return new ModelAndView("filialen/filialen", "filialen", 
                filiaalService.findAll());
    }
    
    @RequestMapping(value="toevoegen", method = RequestMethod.GET)
    public String createForm() {
        return "filialen/toevoegen";
    }   
    @RequestMapping(method = RequestMethod.POST)
    public String create() {
       System.out.println("Filiaal record toevoegen aan database"); 
       return "redirect:/";
    }
    
    @RequestMapping(method = RequestMethod.GET, params="id")
    public ModelAndView read(@RequestParam long id) {
        return new ModelAndView ("filialen/filiaal", "filiaal", 
                filiaalService.read(id));
    }
    
    @RequestMapping (value = "verwijderen", method = RequestMethod.POST, params="id")
    public ModelAndView delete (@RequestParam long id) {
        Filiaal filiaal = filiaalService.read(id);
        if (filiaal == null) {
            return new ModelAndView("redirect:/");
        }
        ModelAndView modelAndView = new ModelAndView("redirect:/filialen/verwijderd", "id", id);
        modelAndView.addObject("naam", filiaal.getNaam());
        filiaalService.delete(id);
        return modelAndView;
    }
    
    @RequestMapping(value="verwijderd", method= RequestMethod.GET, params={"id", "naam"}) 
    public String deleted () {
        return "filialen/verwijderd";
    }
    
    @InitBinder("vantotpostcode")
    public void initBinderVanTotPostcode (DataBinder dataBinder) {
        dataBinder.setRequiredFields("vanpostcode", "totpostcode");
    }
    
    @RequestMapping(value="vantotpostcode", method= RequestMethod.GET)
     public ModelAndView findByPostcodeForm () {
        return new ModelAndView ("filialen/vantotpostcode", "vantotpostcode", 
                new VanTotPostcodeForm());
    }
    /*public ModelAndView findByPostcodeForm () {
        return new ModelAndView ("filialen/vantotpostcode", "vantotpostcode", 
                new VanTotPostcodeForm(1000, 9999));
    }*/
    @RequestMapping(method= RequestMethod.GET, 
            params={"vanpostcode", "totpostcode"})
    public ModelAndView findByPostcode (@ModelAttribute("vantotpostcode") 
            VanTotPostcodeForm vanTotPostcode, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView("filialen/vantotpostcode");
        if (! bindingResult.hasErrors()) {
            if (!vanTotPostcode.isValid()) {
                bindingResult.reject("foutVanTotPostcode",new Object[] {
                    vanTotPostcode.getVanpostcode(), vanTotPostcode.getTotpostcode()},
                        "");
            }
        }
        
        if (! bindingResult.hasErrors()) {
                modelAndView.addObject("filialen",
                filiaalService.findByPostcode(vanTotPostcode.getVanpostcode(),
                vanTotPostcode.getTotpostcode()));
        }       
        return modelAndView;
    }
    
}
